﻿namespace Black_Jack
{
    public class Analizer : Card
    {
        public int combinationValue1,
                   combinationValue2,
                   combinationValue3,
                   oponentCombinationValue1,
                   oponentCombinationValue2,
                   oponentCombinationValue3;

        public int[] player1 = new int[7];
        public int[] player2 = new int[7];

        public void CalculateCombinationValueForServer()
        {
            int p = 0, s = 0, z = 1, w = 0, b = 0;
            int t = 0, a = 0;

            for (int i = 0; i < 7; i++)
            {
                player1[i] = deckCard[i];
            }
            while (p < 7)
            {
                while (z < 7)
                {
                    if (player1[p] > player1[z])
                    {
                        s = player1[z];
                        player1[z] = player1[p];
                        player1[p] = s;
                    }
                    z++;
                }
                p++;
                z = p + 1;
            }

            w = Royal();
            if (w > 99)
            {
                a = w%100;
                w = 8;
            }
            else
            {
                w = kare();
                if (w > 0)
                {
                    a = w/10;
                    b = w%10;
                    w = 7;
                }
                else
                {
                    w = ful();
                    if (w > 99)
                    {
                        a = (w - 100)/10;
                        b = (w - 100)%10;
                        w = 6;
                    }
                    else
                    {
                        t = flush();
                        if (t > 0)
                        {
                            a = t;
                            w = 5;
                        }
                        else
                        {
                            w = stret();
                            if (w > 0)
                            {
                                a = w;
                                w = 4;
                            }
                            else
                            {
                                w = tri();
                                if (w > 10)
                                {
                                    a = w;
                                    w = 3;
                                }
                                else
                                {
                                    w = papa();
                                    if (w > 100)
                                    {
                                        a = w;
                                        w = 2;
                                    }
                                    else
                                    {
                                        w = para();
                                        if (w > 1000)
                                        {
                                            a = w;
                                            w = 1;
                                        }
                                        else
                                        {
                                            w = kiker();
                                            if (w > 1000)
                                            {
                                                a = w;
                                                w = 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            combinationValue1 = w;
            combinationValue2 = a;
            combinationValue3 = b;
        }

        public void CalculateCombinationValueForClient()
        {
            int p = 0, s = 0, z = 1, w = 0, b = 0;
            int t = 0, a = 0;
            for (int i = 0; i < 7; i++)
            {
                player1[i] = deckCard[i + 2];
            }
            while (p < 7)
            {
                while (z < 7)
                {
                    if (player1[p] > player1[z])
                    {
                        s = player1[z];
                        player1[z] = player1[p];
                        player1[p] = s;
                    }
                    z++;
                }
                p++;
                z = p + 1;
            }

            w = Royal();
            if (w > 99)
            {
                a = w%100;
                w = 8;
            }
            else
            {
                w = kare();
                if (w > 0)
                {
                    a = w/10;
                    b = w%10;
                    w = 7;
                }
                else
                {
                    w = ful();
                    if (w > 99)
                    {
                        a = (w - 100)/10;
                        b = (w - 100)%10;
                        w = 6;
                    }
                    else
                    {
                        t = flush();
                        if (t > 0)
                        {
                            a = t;
                            w = 5;
                        }
                        else
                        {
                            w = stret();
                            if (w > 0)
                            {
                                a = w;
                                w = 4;
                            }
                            else
                            {
                                w = tri();
                                if (w > 10)
                                {
                                    a = w;
                                    w = 3;
                                }
                                else
                                {
                                    w = papa();
                                    if (w > 100)
                                    {
                                        a = w;
                                        w = 2;
                                    }
                                    else
                                    {
                                        w = para();
                                        if (w > 1000)
                                        {
                                            a = w;
                                            w = 1;
                                        }
                                        else
                                        {
                                            w = kiker();
                                            if (w > 1000)
                                            {
                                                a = w;
                                                w = 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            oponentCombinationValue1 = w;
            oponentCombinationValue2 = a;
            oponentCombinationValue3 = b;
        }

        public int CalculateResult()
        {
            int p = 0;
            if (combinationValue1 > oponentCombinationValue1)
            {
                p = 1;
            }
            else if (oponentCombinationValue1 > combinationValue1)
            {
                p = 2;
            }
            else if (combinationValue2 > oponentCombinationValue2)
            {
                p = 1;
            }
            else if (oponentCombinationValue2 > combinationValue2)
            {
                p = 2;
            }
            else if (combinationValue3 > oponentCombinationValue3)
            {
                p = 1;
            }
            else if (oponentCombinationValue3 > combinationValue3)
            {
                p = 2;
            }
            else p = 0;

            return (p);
        }

        public int Royal()
        {
            int p = 0, i = 0;

            for (i = 0; i < 3; i++)
            {
                p = player1[i];

                if (((player1[1] == p + 4) || (player1[2] == p + 4) || (player1[3] == p + 4)) && 
                    ((player1[2] == p + 8) || (player1[3] == p + 8) || (player1[4] == p + 8)) && 
                    ((player1[3] == p + 12) || (player1[4] == p + 12) || (player1[5] == p + 12)) && 
                    ((player1[4] == p + 16) || (player1[5] == p + 16) || (player1[6] == p + 16))) 
                    
                    return 100 + p;
            }

            return 0;
        }

        public int kare()
        {
            int i = 0, f = 0, d = 0, h = 0;
            for (i = 0; i < 4; i++)
            {
                if ((player1[i]%4) == 0)
                {
                    if (player1[i + 1] == (player1[i] + 1))
                    {
                        if (player1[i + 2] == (player1[i] + 2))
                        {
                            if (player1[i + 3] == (player1[i] + 3))
                            {
                                f = player1[i];
                                d = player1[i + 3];
                            }
                        }
                    }
                }
            }
            if (d > 0)
            {
                for (i = 0; i < 7; i++)
                {
                    if ((d != player1[i]) && ((d - 1) != player1[i]) && ((d - 2) != player1[i]) && ((d - 3) != player1[i]) && ((d - 4) != player1[i]))
                    {
                        h = player1[i];
                    }
                }
                f = f/4*10 + h/4;
            }

            return (f);
        }

        public int ful()
        {
            int p = 90, i = 0, f = 90, j = 0, e = 0;
            for (i = 0; i < 9; i++)
            {
                e = 0;
                for (j = 6; j > 0; j--)
                {
                    if ((player1[j] >= (i*4)) && (player1[j] < (i*4 + 4)))
                    {
                        e = e + 1;
                    }
                }
                if ((e == 3) && (p == 100))
                {
                    p = i;
                }
                if ((e == 2) && (f == 100))
                {
                    f = i;
                }
            }
            if ((f < 90) && (p < 90))
            {
                f = 100 + p*10 + f;
            }
            return (f);
        }

        public int flush()
        {
            int i = 0, f = 0, j = 5, m = 0;
            int c = 0, v = 1;
            var mas = new int[5];
            for (i = 6; i > 3; i--)
            {
                if (f != 4)
                {
                    j = i - 1;
                    f = 0;
                    m = player1[i]%4;
                    mas[f] = player1[i];
                    while ((f != 4) && (j >= 0))
                    {
                        if ((player1[j]%4) == m)
                        {
                            f++;
                            mas[f] = player1[j];
                        }
                        j--;
                    }
                }
            }
            if (f == 4)
            {
                for (i = 0; i < 5; i++)
                {
                    v = 1;
                    j = 4 - i;
                    while (j > 0)
                    {
                        v = v*10;
                        j--;
                    }
                    c = c + mas[i]/4*v;
                }
            }
            return (c);
        }

        public int stret()
        {
            int i = 0, f = 0, j = 0, e = 1, w = 0;
            var mas = new int[5];
            for (i = 6; i > 3; i--)
            {
                for (j = (i - 1); j >= 0; j--)
                {
                    mas[0] = player1[i]/4;
                    if (player1[i]/4 == player1[j]/4 + e)
                    {
                        f = f + 1;
                        mas[e] = player1[j]/4;
                        e++;
                    }
                }
                e = 0;
                if (f == 4)
                {
                    w = mas[4];
                }
                f = 0;
            }
            return (w);
        }

        public int tri()
        {
            int i = 0, f = 0, j = 0, ere = 0, w = 0, d = 0;
            var mas = new int[5];
            for (i = 0; i < 6; i++)
            {
                f = 0;
                for (j = i + 1; j < 7; j++)
                {
                    if ((player1[i]/4) == (player1[j]/4))
                    {
                        f = f + 1;
                        d = player1[j];
                    }
                    if (f == 2) w = d;
                }
            }
            if (w > 0)
            {
                f = 10;
                ere = w/4*100;
            }
            for (i = 6; i > 0; i--)
            {
                if (((w/4) != (player1[i]/4)) && (f >= 1))
                {
                    ere = ere + f*(player1[i]/4);
                    f = f/10;
                }
            }
            d = ere;
            return (d);
        }

        public int papa()
        {
            int i = 0, f = 0, w = 0, d = 0, k = 0, h = 0;
            var mas = new int[5];
            for (i = 0; i < 6; i++)
            {
                if ((player1[i]/4) == (player1[i + 1]/4))
                {
                    if (d == 1)
                    {
                        f = w;
                        w = player1[i]/4;
                        h = 1;
                    }
                    else
                    {
                        w = player1[i]/4;
                        d = 1;
                    }
                }
            }
            if (h == 1)
            {
                for (i = 6; i > 0; i--)
                {
                    if ((w != (player1[i]/4)) && (f != (player1[i]/4)) && (k == 0))
                    {
                        k = player1[i]/4;
                    }
                }


                w = w*100 + f*10 + k;
            }
            return (w);
        }

        public int para()
        {
            int i = 0, w = 0, d = 0, k = 100;
            var mas = new int[5];
            for (i = 0; i < 6; i++)
            {
                if ((player1[i]/4) == (player1[i + 1]/4))
                {
                    w = player1[i + 1];
                }
            }
            if (w > 0)
            {
                w = w/4;
                for (i = 6; i > 0; i--)
                {
                    if (w != (player1[i]/4))
                    {
                        d = d + k*(player1[i]/4);
                        k = k/10;
                    }
                }
                d = d + w*1000;
            }

            return (d);
        }

        public int kiker()
        {
            int j = 10000, w = 0, i = 0;
            for (i = 6; i > 1; i--)
            {
                w = w + j*(player1[i]/4);
                j = j/10;
            }
            return (w);
        }
    }
}